package manage

import (
	"lingc/models"
	"lingc/services"
	"lingc/utils" // 导入 common 包
	"strings"

	"github.com/gin-gonic/gin"
)

// PageSysBanner 获取所有Banner
func PageSysBanner(c *gin.Context) {

	/*
		Keywords string `json:"keywords" form:"keywords"`
		Area     string `json:"area" form:"area"`
		Status   string `json:"status" form:"status"`
	*/
	var dataRequest struct {
		Keywords string `json:"keywords" form:"keywords"`
		Area     string `json:"area" form:"area"`
		Status   string `json:"status" form:"status"`
		Page     int    `json:"page" form:"page"`
		PageSize int    `json:"pageSize" form:"pageSize"` // Exported field
	}
	// log.Printf("PageUser: %+v", dataRequest)

	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	where := ""
	if dataRequest.Keywords != "" {
		keyword := "'%" + dataRequest.Keywords + "%'"
		where += " `title` like " + keyword
	}
	if dataRequest.Area != "" {
		if where != "" {
			where += " and "
		}
		where += " area = " + dataRequest.Area
	}
	if dataRequest.Status != "" {
		if where != "" {
			where += " and "
		}
		where += " status = " + dataRequest.Status
	}

	list, page, total, err := services.NewSysBannerService(utils.MysqlClient).PageSysBannerList(where, dataRequest.Page, dataRequest.PageSize)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"list": list, "page": page, "total": total})
}

// GetUser 获取Banner
func GetSysBanner(c *gin.Context) {

	var dataRequest struct {
		ID string `json:"id" form:"id"`
	}
	where := ""
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if dataRequest.ID != "" {
		if where != "" {
			where += " and "
		}
		where += " id = " + dataRequest.ID
	}
	data, err := services.NewSysBannerService(utils.MysqlClient).GetSysBanner(where)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// CreateUser 创建Banner
func CreateSysBanner(c *gin.Context) {

	/**
	Title     string `json:"title"`
	Src       string `json:"src"`
	JumpType  string `json:"jump_type"`
	JumpLink  string `json:"jump_link"`
	Area      string `json:"area"`
	Sort      int    `json:"sort"`
	Status    uint8  `json:"status"`
	StartTime string `json:"start_time"`
	EndTime   string `json:"end_time"`
	AppID   int    `json:"app_id"`
	*/
	var createRequest struct {
		Title     string `json:"title" form:"title"`
		Src       string `json:"src" form:"src"`
		JumpType  string `json:"jump_type" form:"jump_type"`
		JumpLink  string `json:"jump_link" form:"jump_link"`
		Area      string `json:"area" form:"area"`
		Sort      int    `json:"sort" form:"sort"`
		Status    uint8  `json:"status" form:"status"`
		StartTime string `json:"start_time" form:"start_time"`
		EndTime   string `json:"end_time" form:"end_time"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysBannerCreate{
		Title:     createRequest.Title,
		Src:       createRequest.Src,
		JumpType:  createRequest.JumpType,
		JumpLink:  createRequest.JumpLink,
		Area:      createRequest.Area,
		Sort:      createRequest.Sort,
		Status:    createRequest.Status,
		StartTime: createRequest.StartTime,
		EndTime:   createRequest.EndTime,
	}
	data, err := services.NewSysBannerService(utils.MysqlClient).CreateSysBanner(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// UpdateUser 更新Banner
func UpdateSysBanner(c *gin.Context) {
	var createRequest struct {
		ID        uint   `json:"id" form:"id"`
		Title     string `json:"title" form:"title"`
		Src       string `json:"src" form:"src"`
		JumpType  string `json:"jump_type" form:"jump_type"`
		JumpLink  string `json:"jump_link" form:"jump_link"`
		Area      string `json:"area" form:"area"`
		Sort      int    `json:"sort" form:"sort"`
		Status    uint8  `json:"status" form:"status"`
		StartTime string `json:"start_time" form:"start_time"`
		EndTime   string `json:"end_time" form:"end_time"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysBannerUpdate{
		ID:        createRequest.ID,
		Title:     createRequest.Title,
		Src:       createRequest.Src,
		JumpType:  createRequest.JumpType,
		JumpLink:  createRequest.JumpLink,
		Area:      createRequest.Area,
		Sort:      createRequest.Sort,
		Status:    createRequest.Status,
		StartTime: createRequest.StartTime,
		EndTime:   createRequest.EndTime,
	}
	data, err := services.NewSysBannerService(utils.MysqlClient).UpdateSysBanner(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// DeleteUser 删除Banner
func DeleteSysBanner(c *gin.Context) {
	var createRequest struct {
		ID  uint   `json:"id" form:"id"`
		IDs string `json:"ids" form:"ids"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID == 0 && len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID != 0 {
		if err := services.NewSysBannerService(utils.MysqlClient).DeleteSysBanner(createRequest.ID); err != nil {
			utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
			return
		}
	}
	if len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	idStrs := strings.Split(createRequest.IDs, ",")
	var ids []string
	for _, idStr := range idStrs {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			continue
		}
		ids = append(ids, idStr)
	}
	// Convert IDs slice to a format suitable for batch deletion
	if len(ids) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "No valid IDs provided"})
		return
	}
	if err := services.NewSysBannerService(utils.MysqlClient).DeleteSysBanners(ids); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	// ... 这里添加删除Banner的逻辑 ...
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"msg": "Delete data"})
}
